<?php
/*******************************************************************************
 * Project: Bridge for WordPress to phpBB 3.0.x
 * File:    functions.php
 * Author:  Jason Sanborn <jsanborn@simplicitypoint.com>
 * Date:    Saturday, July 11, 2009
 * -----------------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

function wpb_get_admin_link()
{
	global $auth, $user, $phpbb_root_path, $phpEx;

	if ($auth->acl_get('a_') && $user->data['is_registered'])
	{
		$url = trim(get_option('wpb_url'));
		$forum_url = str_replace("/ucp.php", "/", $url);
		return $forum_url . 'adm/index.php?sid=' . $user->session_id;
	}
	else
	{
		return '';
	}
}

function wpb_get_avatar()
{
	global $config, $user;

	if(wpb_is_active())
	{
		$url = trim(get_option('wpb_url'));
		$forum_url = str_replace("/ucp.php", "/", $url);

		switch ($user->data['user_avatar_type'])
		{
			case 1:
				return $forum_url . 'download/file.php?avatar=' . $user->data['user_avatar'];
				break;
			case 2:
				return $user->data['user_avatar'];
				break;
			case 3:
				return $forum_url . $config['avatar_gallery_path'] . '/' . $user->data['user_avatar'];
				break;
			default:
				return '';
				break;
		}
	}

	return '';
}

function wpb_get_mcp_link()
{
	global $auth, $user, $phpbb_root_path, $phpEx;

	if ($auth->acl_get('m_') && $user->data['is_registered'])
	{
		$url = trim(get_option('wpb_url'));
		$forum_url = str_replace("/ucp.php", "/", $url);
		return $forum_url . 'mcp.php?i=main&amp;mode=front&amp;sid=' . $user->session_id;
	}
	else
	{
		return '';
	}
}

function wpb_get_restore_permissions_link()
{
	global $user, $auth;

	if ($user->data['user_perm_from'] && $auth->acl_get('a_switchperm'))
	{
		$url = trim(get_option('wpb_url'));
		return $url . '?mode=restore_perm&amp;sid=' . $user->session_id;
	}
	else
	{
		return '';
	}
}

function wpb_get_sessionid()
{
	global $user;
	return $user->session_id;
}

function wpb_get_time_current()
{
	global $user;

	if (wpb_is_active())
	{
		return sprintf($user->lang['CURRENT_TIME'], $user->format_date(time(), false, true));
	}

	return '';
}

function wpb_get_time_last_visit()
{
	global $user;

	if (wpb_is_active() && wpb_is_user_logged_in())
	{
		return sprintf($user->lang['YOU_LAST_VISIT'], $user->format_date($user->data['session_last_visit']));
	}

	return '';
}

function wpb_is_active()
{
	$active = strtolower(trim(get_option('wpb_active')));

	if ($active == 'yes')
	{
		$path = trim(get_option('wpb_path'));

		if (!file_exists($path))
		{
			update_option('wpb_active', 'no');
			return false;
		}

		return true;
	}

	return false;
}

function wpb_is_user_logged_in()
{
	global $user;

	if(wpb_is_active() && $user->data['user_id'] == ANONYMOUS)
	{
		return false;
	}

	return is_user_logged_in();
}

?>